package com.lims.algorithm.algset;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringToInt {
    public static void main(String[] args) {
        System.out.println(strToInt("+1234"));
    }

    public static int strToInt(String str) {
        int number = 0;
        //是否有符号
        int flag = 0;
        //负数为1
        int subflg = 0;
        str.trim();

        if (str == null || "".equals(str)) {
            System.out.println("string is null");
            return 0;
        }

        char[] c = str.toCharArray();
        String newStr = str;
        if (c[0] == '-' || c[0] == '+') {
            flag = 1;
            if (c[0] == '-') subflg = 1;
            newStr = new String(c, 1, c.length - 1);
            System.out.println(newStr);
        }

        Pattern p = Pattern.compile("[a-zA-Z]+");
        Matcher matcher = p.matcher(newStr);
        if (matcher.matches()){
            System.out.println("有字母");
            number = 0;
        }else {
            if (flag == 1){
                for (int i = 1; i<c.length; i++){
                    number = number * 10 + (c[i] - '0');
                }
                if (subflg == 1) number *= -1;
            }else {
                number = Integer.parseInt(newStr);
            }
        }
        return number;
    }
}
